#include<stdio.h>
#include<string.h>

#include "quiz_lib.h"
#include "cmd_line_ui.h"
#include "get_question_and_answer.h"

int main(int i_argc, char* psz_argv[])
{
    struct quiz current_quiz;
    
    /* GUI - welcome, find what quiz user wants to execute*/
    if (get_url(&current_quiz) != 0)
        fprintf(stderr, "Unable to define URL");
    if(get_quiz_properties(&current_quiz) != 0)
        fprintf(stderr, "Unable to define quiz name");
        
    print_quiz_information(&current_quiz);

    struct score current_score;
    struct z_quiz quiz;
    char question[MAX_QUESTION_SIZE];
    char correct_answer[MAX_ANSWER_SIZE];
    char supplied_answer[MAX_ANSWER_SIZE];    

    current_score.possible = 0;
    current_score.awarded = 0;
    
    if(quiz_open(&quiz, current_quiz.url) != 0)
        fprintf(stderr, "Error opening quiz");
    while(quiz_get_question_and_answer(&quiz, question, MAX_QUESTION_SIZE, correct_answer, MAX_ANSWER_SIZE) == 0) {
        
    //strcpy(question, "What is 15 + 3"); //replace w/ xml parser
    //strcpy(correct_answer, "18");       //replace w/ xml parser
    
        if(ask_question(question, supplied_answer) != 0)
            fprintf(stderr, "Error getting answer");
            
        ++current_score.possible;
        if(validate_answer(correct_answer, supplied_answer) == 0) {
            print_success();
            ++current_score.awarded;
        }
        else 
            print_failure(correct_answer);
    }
    if(quiz_close(&quiz) != 0)
        fprintf(stderr, "Error closing quiz");
        
    print_score(&current_score);
    /* GUI - upon completion, return score and restart to welcome screen*/
    
    return(0);
}
